package cn.taokuaidi.wangwang.client.mvc;

import cn.taokuaidi.wangwang.client.constants.AppEvents;
import cn.taokuaidi.wangwang.client.smart.AppConfig;
import cn.taokuaidi.wangwang.client.util.Screen;
import cn.taokuaidi.wangwang.client.widget.BaseEventCenter;
import cn.taokuaidi.wangwang.client.widget.grid.SellerGrid;
import cn.taokuaidi.wangwang.client.widget.toolbar.SellerPagingToolBar;
import cn.taokuaidi.wangwang.client.widget.toolbar.TkdPagingToolBar;

import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.DOM;

public class SellerView extends LayoutContainer {

    private SellerGrid      grid;
    public TkdPagingToolBar toolBar;

    @Override
    protected void onRender(com.google.gwt.user.client.Element parent, int index) {
        super.onRender(parent, index);
        init();
    }

    private void loadData() {
    }

    private boolean isExtendTime() {
        String value = DOM.getElementById("isAPIExtendTime").getAttribute("value");
        if ("false".equalsIgnoreCase(value)) {
            return true;
        }
        return false;
    }

    public void init() {
        Dispatcher.get().addController(new BaseEventCenter() {

            @Override
            public void handleEvent(AppEvent<?> event) {
                if (event.type == AppEvents.SELLER_DATA_LOADED) {
                    loadData();
                }
            }
        });
        // 初始化grid并获得数据
        grid = new SellerGrid(null, null);
        grid.initData();
        AppConfig appConfig = Screen.getAppConfig();
        // 关联分页组件
        toolBar = new SellerPagingToolBar(appConfig.sellerGridPageSize);
        ContentPanel tipPanel = new ContentPanel();
        tipPanel.setHeaderVisible(false);
        tipPanel.setIconStyle("incon-text");
        tipPanel.addText("<font color='#FF6600' size=2>亲爱的用户，如果未出现最新订单，是由于淘宝API访问的时效引起的，需要您重新授权并刷新本页，您可能需要再次输入淘宝网的用户名与密码，并对我们给予授权确认，谢谢。<a href='/gwt_seller/login_taobao'>点击此处授权</a></font>");
        if (isExtendTime()) {
            toolBar.setPageSize(15);
            add(tipPanel);
        }

        ContentPanel panel = new ContentPanel();
        panel.setLayout(new FitLayout());
        panel.add(grid);
        panel.setHeaderVisible(false);
        panel.setHeight(550);
        panel.setTopComponent(toolBar);
        add(panel);

    }
}
